package com.zat.mes.andrmes.dofun.main.vm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.HomeBtnBean
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.data.MessageBean
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import com.zat.mes.andrmes.utils.PreferencesUtil
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-02-07 00:05
 * @description:主页model
 */
class HomeModel : ViewModel() {

    private val homeBtnData: MutableLiveData<MutableList<HomeBtnBean>> by lazy {
        MutableLiveData<MutableList<HomeBtnBean>>().also {
            loadMenu()
        }
    }
    private val messageData: MutableLiveData<MessageBean> by lazy {
        MutableLiveData<MessageBean>().also {
            val loginResultBean = Gson().fromJson(
                PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
                LoginResultBean::class.java
            )
            val empId = loginResultBean.empid
            loadComList(empId.toString())
        }
    }

    fun getMessageList(): LiveData<MessageBean> {
        return messageData
    }

    fun getHomeBtnList(): LiveData<MutableList<HomeBtnBean>> {
        return homeBtnData
    }

    /**
     * 获取通知公告
     */
    private fun loadComList(empId:String) {
        RetrofitManager.run {
            apiService(ApiService::class.java).getMessage(empId)
                .enqueue(object : MesCallBack<BaseResponse<MessageBean>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<MessageBean>>?,
                        response: Response<BaseResponse<MessageBean>>?
                    ) {
                        var comResultBeanList = response?.body()?.result
                        messageData.postValue(comResultBeanList)
                    }

                })
        }
    }

    /**
     * 获取首页菜单
     */
    private fun loadMenu() {
        val loginResultBean: LoginResultBean? = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        loginResultBean?.empid?.let {
            RetrofitManager.apiService(ApiService::class.java)
                .getSysMenu(it)
                .enqueue(object : MesCallBack<BaseResponse<List<HomeBtnBean>>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<List<HomeBtnBean>>>?,
                        response: Response<BaseResponse<List<HomeBtnBean>>>?
                    ) {
                        val homeBtnBeans = response?.body()?.result
                        homeBtnData.postValue(homeBtnBeans as MutableList<HomeBtnBean>?)
                    }

                })
        }
    }


}